/*
 * ProtocolSender.cpp
 *
 *  Created on: Sep 8, 2017
 *      Author: guoxs
 */

#include "uart/ProtocolSender.h"
#include "uart/UartContext.h"
#include "utils/Log.h"
#include <stdio.h>

extern BYTE getCheckSum(const BYTE *pData, int len);

/**
 * 需要根据协议格式进行拼接，以下只是个模板
 * Need to be spliced ​​according to the agreement format, the following is just a template
 */
bool sendProtocol(const UINT16 cmdID, const BYTE *pData, BYTE len)
{
   if (len + DATA_PACKAGE_MIN_LEN > 256)
   {
      LOGE("sendProtocol data is too len !!!\n");
      return false;
   }

   BYTE dataBuf[256];

   dataBuf[0] = CMD_HEAD1;
   dataBuf[1] = CMD_HEAD2; // 同步帧头 Sync frame header

   dataBuf[2] = HIBYTE(cmdID);
   dataBuf[3] = LOBYTE(cmdID); // 命令字节 Command byte

   dataBuf[4] = len;

   UINT frameLen = 5;

   // 数据 Data
   for (int i = 0; i < len; ++i)
   {
      dataBuf[frameLen] = pData[i];
      frameLen++;
   }

#ifdef PRO_SUPPORT_CHECK_SUM
   // 校验码 Checksum
   dataBuf[frameLen] = getCheckSum(dataBuf, frameLen);
   frameLen++;
#endif

   return UARTCONTEXT->send(dataBuf, frameLen);
}

bool send_7F()
{
   BYTE dataBuf[5];
   dataBuf[0] = 0x7F;
   dataBuf[1] = 0x7F;
   dataBuf[2] = 0x7F;
   dataBuf[3] = 0x7F;
   dataBuf[4] = 0x7F;
   return UARTCONTEXT->send(dataBuf, 1);
}

bool send_UART(BYTE *pdata, BYTE len)
{
   // BYTE dataBuf[256];
   // dataBuf[0] = 0x7F;
   // dataBuf[1] = 0x7F;
   // dataBuf[2] = 0x7F;
   // dataBuf[3] = 0x7F;
   // dataBuf[4] = 0x7F;
   return UARTCONTEXT->send(pdata, len);
}
